package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.ManagerBean;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IManagerService;
import com.easy.common.util.PasswordUtil;
import com.easy.common.util.SYS;
import com.easy.common.util.TokenUtil;
import com.easy.user.mapper.ManagerMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, ManagerBean> implements IManagerService {

    @Override
    public ManagerBean getManagerById(Integer id) {
        return super.getById(id);
    }

    @Override
    public ManagerBean getManagerByNickName(String managernickname) {
        LambdaQueryWrapper<ManagerBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ManagerBean::getManagernickname, managernickname);
        return super.getOne(queryWrapper);
    }

    @Override
    public int addManager(ManagerBean manager) {
        if (super.save(manager)) {
            return 1;
        }
        return 0;
    }

    @Override
    public int uploadManager(ManagerBean manager) {
        return super.updateById(manager)?1:0;
    }

    @Override
    public int deleteManager(Integer id) {
        return super.removeById(id)?1:0;
    }

    @Override
    public int reverseStatus(Integer id) {
        ManagerBean manager = super.getById(id);
        if(manager!=null){
            manager.setIsdel(manager.getIsdel()==1?0:1);
        }
        if(super.updateById(manager)){
            return 1;
        }
        return 0;
    }

    @Override
    public List<ManagerBean> getAll(ManagerBean manager) {
        LambdaQueryWrapper<ManagerBean> queryWrapper = new LambdaQueryWrapper<>();
        return super.list(queryWrapper);
    }
    @Override
    public IPage<ManagerBean> getPage(ManagerBean manager, Page<ManagerBean> page) {
        LambdaQueryWrapper<ManagerBean> queryWrapper = new LambdaQueryWrapper<>();
        if(manager!=null&& manager.getManagerid()!=null&& !manager.getManagerid().isEmpty()) {
            queryWrapper.like(ManagerBean::getManagerid, manager.getManagerid());
        }
        if(manager!=null&& manager.getManagernickname()!=null&& !manager.getManagernickname().isEmpty()) {
            queryWrapper.like(ManagerBean::getManagernickname, manager.getManagernickname());
        }
        return super.page(page,queryWrapper);
    }
    // 新增：管理员登录方法
    @Override
    public String adminLogin(String managerid, String password) {
        System.out.println(managerid);
        System.out.println(password);
        LambdaQueryWrapper<ManagerBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ManagerBean::getManagerid, managerid);
        ManagerBean manager = super.getOne(queryWrapper);
        System.out.println(manager);
        if (manager == null) {
            throw new RuntimeException("管理员账号不存在");
        }

        // 验证密码
        if (!PasswordUtil.checkPassword(password, manager.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        // 返回token，标记用户类型为admin
        return TokenUtil.getToken(manager.getId().toString(), "admin");
    }

    // 新增：通过token获取管理员信息
    @Override
    public ManagerBean getManagerByToken(String token) {
        Object id = TokenUtil.getOption(token, SYS.TOKEN_MANAGER_ID);
        if (id == null) {
            throw new RuntimeException("token中无管理员信息");
        }
        return super.getById(Integer.parseInt(id.toString()));
    }

}
